package actions;

import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.CachedRowSet;

import model.Applicant;
import model.App;
import model.Database;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.GetApplicationsForm;

public class GetApplicationsAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws DbException, SQLException, ClassNotFoundException {
		GetApplicationsForm iForm = (GetApplicationsForm)form;
		
		/*
		Vector<Integer> averageGrade = new Vector<Integer>();
		Vector<String> applicantName = new Vector<String>();
		Vector<String> appStatus = new Vector<String>(); 
		*/
		
		Vector<App> applicationVector = new Vector<App>();
		
		CachedRowSet applications;
		if(iForm.isGraded()){
			applications = Database.getGradedByReviewer(iForm.getReviewer());
		}else{
			applications = Database.getNotGradedByReviewer(iForm.getReviewer());
		}
		
		while(applications.next()){
			App application = new App();
			int id = applications.getInt("id");
			
			application.setUserId(id);

			application.setAverageGrade(Database.getAverageGrade(id));
			
			Applicant applicant = Database.getApplicantById(id);
			application.setFirstName(applicant.getFirst_name());
			application.setLastName(applicant.getLast_name());
			application.setAppStatus(Database.getAppStatus(id));
			
			applicationVector.add(application);
		}
		 
		HttpSession session = request.getSession();
		session.setAttribute("applicationVector", applicationVector);
		return mapping.findForward("success");
	}

}
